package com.gl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.entity.CrmBanner;
import com.gl.entity.EduCourse;
import com.gl.entity.EduTeacher;
import com.gl.service.CrmBannerService;
import com.gl.service.EduCourseService;
import com.gl.service.EduTeacherService;
import com.gl.utils.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/banner")
public class CrmBannerController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private CrmBannerService bannerService;
    //首页轮播图
    @GetMapping("index")
    public List<CrmBanner> index(){
        //取id最前3条
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 3");
        List<CrmBanner> list = bannerService.list(wrapper);
        return list;

    }



    //获取首页热门课程和专业讲师
    @GetMapping("getCourseTeacher")
    public Map getCourseTeacher(){
        //获取id降序9门热门课程
        QueryWrapper<EduCourse> wrapperCourse = new QueryWrapper<>();
        wrapperCourse.orderByDesc("id");
        wrapperCourse.last("limit 9");
        List<EduCourse> listCourse = courseService.list(wrapperCourse);
        //获取id降序4位讲师
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<EduTeacher> listTeacher = teacherService.list(wrapperTeacher);

        Map map = new HashMap();
        map.put("courseList",listCourse);
        map.put("teacherList",listTeacher);
        return map;


    }

    //最新课程轮播生成
    @GetMapping("createBanner")
    public CommonResponse createBanner(){
        bannerService.createBanner();
        return CommonResponse.ok();
    }

    //后台banner图
    @GetMapping("getBackBanner")
    public CommonResponse getBackBanner(){

        List<CrmBanner> list = bannerService.list(null);
        return CommonResponse.ok().data("list",list);
    }



}

